/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.Desktop;
import com.kylingroup.Desktop.Database.DBConnection;
import com.kylingroup.Desktop.Database.DBLocalSQLite;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 *
 * @author kylin
 */
public class MainFrame extends Application{
    
    public Stage mainFrame = null;
    private Group root = new Group();
    private BorderPane maincontent = null;
    private ApplicationContext applicationContext;
    public static DBConnection dbconn=null;
    
    private double maxwidth,maxheight,prewidth=1000,preheight=500;
    private boolean fmax,fmin;

    public void appContext(){
        applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        dbconn=(DBLocalSQLite)applicationContext.getBean("DBConnection");
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        appContext();
        
        this.mainFrame=primaryStage;
        
        Toolkit tk=Toolkit.getDefaultToolkit();
        Dimension screen=tk.getScreenSize();
        maxwidth=screen.getWidth();
        maxheight=screen.getHeight()-100;

        
        
        mainFrame.setScene(new Scene(createContent()));
        mainFrame.centerOnScreen();
        
        mainFrame.maximizedProperty().addListener(new ChangeListener(){
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                fmax=(boolean)newValue;
                if((boolean)newValue){
                    Platform.runLater(new Runnable(){
                        @Override
                        public void run() {
                            maincontent.prefWidthProperty().setValue(maxwidth);
                            maincontent.prefHeightProperty().setValue(maxheight);
                        }
                    });
                }else{
                    Platform.runLater(new Runnable(){
                        @Override
                        public void run() {
                            maincontent.prefWidthProperty().setValue(prewidth);
                            maincontent.prefHeightProperty().setValue(preheight);  
                        }
                    });
                }
            }
        });
        
        mainFrame.iconifiedProperty().addListener(new ChangeListener(){
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                if((boolean)newValue){
                    
                }else{
                    if(fmax){
                        Platform.runLater(new Runnable(){
                            @Override
                            public void run() {
                                maincontent.prefWidthProperty().setValue(maxwidth);
                                maincontent.prefHeightProperty().setValue(maxheight);
                                
                            }
                        });
                    }else{
                        Platform.runLater(new Runnable(){
                            @Override
                            public void run() {
                                maincontent.prefWidthProperty().setValue(prewidth);
                                maincontent.prefHeightProperty().setValue(preheight);
                            }
                        });
                    }
                }
            }
        });
        
        mainFrame.show();
    }
 
    
    public static void main(String[] args) {
        launch(args);
    }
    
    private Parent createContent(){
        FXMLLoader loader = new FXMLLoader();
        InputStream in = MainFrame.class.getResourceAsStream("MainFrame.fxml");
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(MainFrame.class.getResource("MainFrame.fxml"));

        try {
            maincontent = (BorderPane) loader.load(in);
            maincontent.setMaxWidth(1366);
            MainFrameController maincontrol=(MainFrameController)loader.getController();
            maincontrol.setApp(this);
            
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        root.getChildren().removeAll();
        root.getChildren().addAll(maincontent);
        return root;
    }
}
